function [drate_nominal_diff,yy,eq_final,d2c]=calibration_func_v3(A,otherpar,psiz_grid,com_opt)
%================================================
% comp_opt = 0 if perfect competition in loan market
%              otherwise Cournot competition  
%==============================================
%==========================================
%Define parameters and functions
%==========================================
beta = otherpar.beta;
cost = otherpar.cost;
pi_eff=otherpar.pi_eff; % effective inflation for bank reserves 1+pi_eff = (1+pi_m)/(1+i_r)
pi_m=otherpar.pi_m; % effective inflation for bank reserves 1+pi_eff = (1+pi_m)/(1+i_r)

alpha2 = otherpar.alpha2;
alpha3 = otherpar.alpha3;
spread = otherpar.spread;
lambda_func = otherpar.lambda_func;
eta =otherpar.eta;

Nb_grid=1:500;

if com_opt==1
    df_inv = @(rho,Nb)(A*eta*(1-(1-eta)/Nb)./(1+rho)).^(1/(1-eta));
    df=@(k,Nb)A*eta*(1-(1-eta)/Nb)*k.^(eta-1); %marginal production
else
    df_inv = @(rho)(A*eta./(1+rho)).^(1/(1-eta));
    df=@(k)A*eta*k.^(eta-1); %marginal production
end
for i=1:length(Nb_grid)
    Nb=Nb_grid(i);

    otherpar.df_inv = df_inv;
    otherpar.df=df;
    y=SS_eq_noCBDC(otherpar,pi_eff,Nb,psiz_grid );
    eq_final(i,:)=y;
    psi= beta*(alpha2*lambda_func(eq_final(i,2))+alpha3*lambda_func(sum(eq_final(i,[1,2]),2)))+beta;
    drate(i)=1./psi-1;
    if com_opt==1
        lending_rate(i) = (1+eq_final(i,3))/(1-(1-eta)/Nb)-1;
    else
        lending_rate(i) = eq_final(i,3);
    end
    a(i)= lending_rate(i)-drate(i)-cost;

    if (1+lending_rate(i))*(1+otherpar.pi_m)-(1+drate(i))*(1+otherpar.pi_m)<spread
            
            yy=Nb;

        break;
    end
end
d2c = eq_final(yy,2)*psi/eq_final(yy,1);
drate_nominal_diff = (drate(i)+1)*(1+pi_m)-1 - otherpar.i_D;

